package com.media.output;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;

import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/media")
public class MediaController {
	/**@GetMapping(value = "/flv/{deviceId}_{channelId}.flv")
    public Flux<String> flv(@PathVariable String deviceId,@PathVariable String channelId){
        System.out.println(deviceId);
        System.out.println(channelId);
		return Flux.interval(Duration.ofMillis(500))
				.map(String -> "1");
    }**/
	@GetMapping(value = "/flv/{deviceId}_{channelId}.flv")
	public ResponseEntity<byte[]> flv(@PathVariable String deviceId,@PathVariable String channelId){
		try {
			//File tempFile = new File("classpath:test.flv");
			//FileInputStream fis = new FileInputStream(tempFile);
			InputStream fis=new ClassPathResource("test.flv").getInputStream();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1)
			{
			    bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); //二进制或流数据
			ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(bos.toByteArray(), httpHeaders, HttpStatus.CREATED);
			return filebyte;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
